package com.boot.provider.config;

import com.ego.inter.service.UserService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 服务提供者
 * @date 2020/10/13 8:35
 */
@Configuration
public class DubboConfig {

    /**
     * 声明服务名称
     * @return config
     */
    @Bean
    public ApplicationConfig applicationConfig(){
        ApplicationConfig config = new ApplicationConfig("boot-provider");
        return config;
    }

    /**
     * 声明注册中心配置
     * @return config
     */
    @Bean
    public RegistryConfig registryConfig(){
        RegistryConfig config = new RegistryConfig("zookeeper://localhost:2181");
        return config;
    }

    /**
     * 声明暴露协议和端口
     * @return config
     */
    @Bean
    public ProtocolConfig protocolConfig(){
        ProtocolConfig config = new ProtocolConfig("dubbo",20880);
        return config;
    }

    /**
     * 暴露UserServiceImpl服务
     * @param userService
     * @param applicationConfig
     * @param registryConfig
     * @return
     */
    @Bean
    public ServiceConfig<UserService> serviceConfigUserService(UserService userService,
                                                               ApplicationConfig applicationConfig,
                                                               RegistryConfig registryConfig){
//        声明配置器
        ServiceConfig<UserService> serviceConfig = new ServiceConfig<>();
//        声明想要暴露的接口
        serviceConfig.setInterface(UserService.class);
//        指定暴露的接口对象
        serviceConfig.setRef(userService) ;
//        指定应用程序的名称
        serviceConfig.setApplication(applicationConfig);
//        设置注册中心
        serviceConfig.setRegistry(registryConfig);
//        暴露出去
        serviceConfig.export();
        return serviceConfig;
    }
}
